#pragma once
#include "../Misc/resource.h"
#include "../Disks/DiskMgmt.h"
#include "Settings.h"

class CNotifyWnd:
	public CWindowImpl<CNotifyWnd>
{
	static const UINT WM_ICON_NOTIFY = WM_APP + 10;
	static const UINT WM_TASKBAR_CREATED;
public:
	inline CNotifyWnd() 
	{
		m_bConfiguring = false;
	}
	inline ~CNotifyWnd() { }

	HRESULT Create( IVdsService *pService, HINSTANCE hInstance, LPCTSTR lpMenuName );

	DECLARE_WND_CLASS_EX( L"CNotifyWnd", CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, NULL );

	BEGIN_MSG_MAP( CNotifyWnd )
		// MESSAGE_HANDLER( WM_CREATE, onCreate )
		MESSAGE_HANDLER( WM_DESTROY, onDestroy )
		MESSAGE_HANDLER( WM_TASKBAR_CREATED, onTaskbarCreated );
		MESSAGE_HANDLER( WM_ICON_NOTIFY, onIconNotify );
		MESSAGE_HANDLER( WM_DEVICECHANGE, onDeviceChange );

		COMMAND_ID_HANDLER( ID_TRAY_ONLINE, actOnline );
		COMMAND_ID_HANDLER( ID_TRAY_CONFIGURE, actConfigure );
		COMMAND_ID_HANDLER( ID_TRAY_EXIT, actExit );
	END_MSG_MAP()

private:
	virtual void OnFinalMessage( _In_ HWND )
	{
		PostQuitMessage( 0 );
	}

	HINSTANCE m_hInstance;
	UINT m_uCreationFlags;
	NOTIFYICONDATA  m_tnd;

	HMENU m_menu, m_submenu;
	HICON iconActive, iconOffline;

	// LRESULT onCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled );
	LRESULT onDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled );

	LRESULT onTaskbarCreated( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled );
	LRESULT onIconNotify( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled );

	LRESULT actOnline( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled );
	LRESULT actConfigure( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled );
	LRESULT actExit( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled );

	void updateTooltip();

	void customizeMenu();

	CComPtr<IVdsService> m_pService;

	LRESULT onDeviceChange( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
	{
		listDevices();
		updateTooltip();
		return 0;
	}

	HRESULT listDevices();

	CAtlArray<CComPtr<IVdsDisk>> m_drives;
	CComPtr<IVdsDisk> m_defaultDrive;
	CString m_strTooltip;
	bool m_bNowOnline;
	bool m_bConfiguring;
};